package com.webbuilder.task;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.jiusit.golf.api.HtmlParser;
import com.jiusit.golf.api.LotteryUtil;
import com.webbuilder.common.Var;
import com.webbuilder.utils.DateUtil;
import com.webbuilder.utils.DbUtil;
import com.webbuilder.utils.StringUtil;
import com.webbuilder.utils.SysUtil;

public class ConfirmReceipt implements Job {
	public void execute(JobExecutionContext context)
			throws JobExecutionException {
		System.out.println("开始进行定时确认收货");
		Connection conn = null;
		PreparedStatement st = null;
		PreparedStatement sel_st = null;
		PreparedStatement selDays_st = null;
		PreparedStatement updateTotal_st = null;
		PreparedStatement updateDetail_st = null;
		PreparedStatement updateAgent_st = null;
		ResultSet sel_rs = null;
		ResultSet selDays_rs = null;
			try {
				conn = DbUtil.getConnection();
				conn.setAutoCommit(false);
				String daysSql="select * from wb_var where VAR_NAME='confirm_days'";
				selDays_st=conn.prepareStatement(daysSql);
				selDays_rs=selDays_st.executeQuery();
				if(selDays_rs.next()){
					if(!StringUtil.isEmpty(selDays_rs.getString("VAR_VALUE"))){
						//更新购买总表
						String updateTotalSql=" update purchase_commodity_buy_total set order_status='08',receipt_date=now() where id= ? ";
						updateTotal_st=conn.prepareStatement(updateTotalSql);
						//更新购买明细表
						String updateDetailSql="update purchase_commodity_buy set order_status='08' where total_id= ? ";
						updateDetail_st=conn.prepareStatement(updateDetailSql);
						//更新代理商收益
						String updateAgentSql="update trade_market_user set trade_profit=trade_profit+? where id= ? ";
						updateAgent_st=conn.prepareStatement(updateAgentSql);
						//插入代理商收益表
						String inertSql="insert into trade_market_profit(id,agent_id,total_id,agent_profit,transfer_date,transfer_year,transfer_month,create_by,create_name,create_date,update_by,update_name,update_date,remarks)";
						inertSql+=" values(?,?,?,?,now(),?,?,'admin','admin',now(),'admin','admin',now(),'定时任务确认收货')";
						st=conn.prepareStatement(inertSql);
					int days=selDays_rs.getInt("VAR_VALUE");
					days=days*24*60*60;
					String commodityOrderSql="select * from purchase_commodity_buy_total where order_status='03'  and UNIX_TIMESTAMP(NOW())-UNIX_TIMESTAMP(deliver_date) >= "+days+" ";
					sel_st=conn.prepareStatement(commodityOrderSql);
					sel_rs=sel_st.executeQuery();
					while(sel_rs.next()){
						String id=SysUtil.getUUID();
						GregorianCalendar g=new GregorianCalendar();
						int year=g.get(GregorianCalendar.YEAR);
						int month=g.get(GregorianCalendar.MONTH)+1;
						updateTotal_st.setString(1, sel_rs.getString("id"));
						updateDetail_st.setString(1, sel_rs.getString("id"));
						if(sel_rs.getFloat("agent_total_revenue") >0 && !StringUtil.isEmpty(sel_rs.getString("agent_id"))){
						updateAgent_st.setFloat(1, sel_rs.getFloat("agent_total_revenue"));
						updateAgent_st.setString(2, sel_rs.getString("agent_id"));
						st.setString(1, id);
						st.setString(2, sel_rs.getString("agent_id"));
						st.setString(3, sel_rs.getString("id"));
						st.setFloat(4, sel_rs.getFloat("agent_total_revenue"));
						st.setInt(5, year);
						st.setInt(6, month);
						updateAgent_st.addBatch();
						st.addBatch();
						}
						updateTotal_st.addBatch();
						updateDetail_st.addBatch();
						
					  }
					st.executeBatch();
					updateAgent_st.executeBatch();
					updateDetail_st.executeBatch();
					updateTotal_st.executeBatch();
					}
				}
			} catch (Throwable e) {
				DbUtil.closeConnection(conn, true);
			} finally {
				DbUtil.closeStatement(st);
				DbUtil.closeStatement(sel_st);
				DbUtil.closeStatement(updateAgent_st);
				DbUtil.closeStatement(updateDetail_st);
				DbUtil.closeStatement(updateTotal_st);
				DbUtil.closeStatement(selDays_st);
				DbUtil.closeResultSet(sel_rs);
				DbUtil.closeResultSet(selDays_rs);
				DbUtil.closeConnection(conn, false);
			}
	}
}